package com.muyu.danmo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoArtColleges;
import com.muyu.danmo.domain.dto.DepartmentDTO;
import com.muyu.danmo.req.DanmoArtCollegesReq;
import com.muyu.danmo.service.IDanmoArtCollegesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 书画院校Controller
 *
 * @author yangle
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/artColleges")
public class DanmoArtCollegesController extends BaseController {
    @Autowired
    private IDanmoArtCollegesService danmoArtCollegesService;

    /**
     * 查询书画院校列表
     */
//    @RequiresPermissions("system:artColleges:list")
    @PostMapping("/list")
    public Result<PageInfo<DanmoArtColleges>> list(@RequestBody DanmoArtColleges danmoArtColleges) {

        PageHelper.startPage(danmoArtColleges.getPageNum(), danmoArtColleges.getPageSize());
        List<DanmoArtColleges> list = danmoArtCollegesService.selectDanmoArtCollegesList(danmoArtColleges);
        PageInfo<DanmoArtColleges> danmoArtCollegesPageInfo = new PageInfo<>(list);
        return Result.success(danmoArtCollegesPageInfo);
    }

    /**
     * 导出书画院校列表
     */
//    @RequiresPermissions("system:artColleges:export")
    @Log(title = "书画院校", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoArtColleges danmoArtColleges) {
        List<DanmoArtColleges> list = danmoArtCollegesService.selectDanmoArtCollegesList(danmoArtColleges);
        ExcelUtil<DanmoArtColleges> util = new ExcelUtil<DanmoArtColleges>(DanmoArtColleges.class);
        util.exportExcel(response, list, "书画院校数据");
    }

    /**
     * 获取书画院校详细信息
     */
//    @RequiresPermissions("system:artColleges:query")
    @GetMapping(value = "selectArtColleges/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoArtCollegesService.selectDanmoArtCollegesById(id);
    }

    /**
     * 新增书画院校
     */
//    @RequiresPermissions("system:artColleges:add")
    @Log(title = "书画院校", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoArtColleges danmoArtColleges) {
        return danmoArtCollegesService.insertDanmoArtColleges(danmoArtColleges);
    }

    /**
     * 修改书画院校
     */
//    @RequiresPermissions("system:artColleges:edit")
    @Log(title = "书画院校", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public Result edit(@RequestBody DanmoArtColleges danmoArtColleges) {
        return danmoArtCollegesService.updateDanmoArtColleges(danmoArtColleges);
    }

    /**
     * 删除书画院校
     */
//    @RequiresPermissions("system:artColleges:remove")
    @Log(title = "书画院校", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return danmoArtCollegesService.deleteDanmoArtCollegesByIds(ids);
    }

    /**
     * 查看自己收藏的院校
     */
    @PostMapping("/selectUid")
    public Result selectUid() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoArtCollegesService.selectUid(loginUser.getUserid());

    }


    /**
     * 收藏院校
     */
    @GetMapping("/collectionColleges/{id}")
    public Result collectionColleges(@PathVariable Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        return danmoArtCollegesService.isCollect(id, userId);
    }

    /**
     * 取消收藏院校
     */
    @DeleteMapping("/removeCollectionColleges/{ids}")
    public Result removeCollectionColleges(@PathVariable Long[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        return danmoArtCollegesService.removeArtCollectionColleges(ids, userId);
    }


    /**
     * 筛选 学院特色 以及 办学层次  以及 办学类型
     */
    @PostMapping("/queryColleges")
    public Result<PageInfo<DanmoArtColleges>> queryColleges(@RequestBody DanmoArtCollegesReq req) {

        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<DanmoArtColleges> danmoArtColleges = danmoArtCollegesService.queryColleges(req);
        PageInfo<DanmoArtColleges> danmoArtCollegesPageInfo = new PageInfo<>(danmoArtColleges);
        return Result.success(danmoArtCollegesPageInfo);
    }

    /**
     * 院系和专业关联
     *
     * @return
     */
    @GetMapping("/processMajors/{id}")
    public Result processMajors(@PathVariable Long id) {
        Map<String, List<DepartmentDTO>> danmoArtDepartmentsList =   danmoArtCollegesService.selectDepartmentsList(id);
       return Result.success(danmoArtDepartmentsList);
    }
}
